package ru.sgu.codecenter.fidx.model;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import ru.sgu.codecenter.fidx.Configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class FileDocument implements Comparable<FileDocument> {
    public static final String[] SKIPPING_EXTENSIONS = Configuration.getSkippingExtensions();

    public static final int FILE_READ_BUFFER = Configuration.getMaxFileSize();
    public static final int MAX_FILE_SIZE = Configuration.getMaxFileSize();

    private String path;
    private String name;
    private String content;
    private String sha1;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSha1() {
        return sha1;
    }

    public void setSha1(String sha1) {
        this.sha1 = sha1;
    }

    public static boolean skipFileByExtension(String extension) {
        for (String skipExtension : SKIPPING_EXTENSIONS) {
            if (extension.equalsIgnoreCase(skipExtension)) {
                return true;
            }
        }
        return false;
    }

    public static FileDocument newFileDocument(File file) throws IOException {
        FileReader fileReader = new FileReader(file);
        StringBuilder content = new StringBuilder();

        if (!skipFileByExtension(FilenameUtils.getExtension(file.getName()))) {
            try {
                char[] buffer = new char[FILE_READ_BUFFER];
                while (true) {
                    int readCount = fileReader.read(buffer);
                    if (readCount == -1) {
                        break;
                    }
                    content.append(buffer, 0, readCount);
                    if (content.length() >= MAX_FILE_SIZE) {
                        break;
                    }
                }
            } finally {
                fileReader.close();
            }
        }

        FileInputStream fileInputStream = new FileInputStream(file);

        FileDocument fileDocument = new FileDocument();
        fileDocument.setPath(file.getCanonicalPath());
        fileDocument.setName(file.getName());
        fileDocument.setContent(content.toString());
        fileDocument.setSha1(DigestUtils.sha1Hex(fileInputStream));

        fileInputStream.close();

        return fileDocument;
    }

    @Override
    public int compareTo(FileDocument o) {
        if (o == null) {
            return 1;
        }

        return getPath().compareTo(o.getPath());
    }
}
